//
//  UIButton+IconObserver.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/4/20.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "UIButton+IconObserver.h"
#import "IconContext.h"
#import "UIImageView+WebCache.h"
#import <objc/runtime.h>
#import "MJExtension.h"
#import "UIButton+FadeWebCache.h"

#ifndef kIconObserverPlaceHolder
#define kIconObserverPlaceHolder @"YijuDefault.jpg"
#endif

#ifndef kIconFadeTime
#define kIconFadeTime 0.5f
#endif

static const void *iconNameKey = &iconNameKey;
static const void *placeHolderNameKey = &placeHolderNameKey;
static const void *controlStateKey = &controlStateKey;

@implementation UIButton (IconObserver)

/**
 *  产生icon的完全地址
 *
 *  @param iconPath 服务器传送过来的icon相对路径
 *
 *  @return icon的绝对http路径
 */
- (NSString *)generateIconPath:(NSString *)iconPath{
    NSString *iconUrl = iconPath;
    if (![iconPath containsString:@"http:"]) {
        iconUrl = [[IconContext sharedInstance] iconByName:iconPath];
    }
    return iconUrl;
}

/**
 *  更新当前的图片
 */
- (void)updateImage{
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    IconContext *iconContext = [IconContext sharedInstance];
    NSString *iconPath = [iconContext valueForKey:self.iconKey];
    iconPath = [self generateIconPath:iconPath];
    [self setImageWithURLString:iconPath andPlaceHolderString:self.placeHolderName forState:self.controlState];
}

#pragma mark - Public
/**
 *  设置需要现实的icon
 *
 *  @param iconName        icon的名字，应该与IconContext 里面的属性相同
 *  @param placeHolderName 默认图片，给定名字
 */
- (void)setIconWithKey:(NSString *)iconKey withPlaceHolderString:(NSString *)placeHolderName forState:(UIControlState)state{
    if (iconKey == nil) {
        return;
    }
    IconContext *iconContext = [IconContext sharedInstance];
    NSDictionary *iconDic = [iconContext mj_keyValuesWithIgnoredKeys:@[@"loadManager",@"checkManager"]];
    
    if (![[iconDic allKeys] containsObject:iconKey]) {
        DDLogWarn(@"当前iconKey:%@在IconContext未找到,请设置正确的iconKey",iconKey);
        return;
    }
    self.iconKey = iconKey;
    self.placeHolderName = placeHolderName;
    self.controlState = state;
    
    DDLogInfo(@"iconName:%@",self.iconKey);
    [self updateImage];
}

#pragma mark - Setter
@dynamic iconKey;
- (void)setIconKey:(NSString *)iconKey{
    objc_setAssociatedObject(self, iconNameKey, iconKey, OBJC_ASSOCIATION_COPY);
}

@dynamic placeHolderName;
- (void)setPlaceHolderName:(NSString *)placeHolderName{
    objc_setAssociatedObject(self, placeHolderNameKey, placeHolderName, OBJC_ASSOCIATION_COPY);
}

@dynamic controlState;
- (void)setControlState:(UIControlState)controlState{
    objc_setAssociatedObject(self, controlStateKey, @(controlState), OBJC_ASSOCIATION_RETAIN);
}

#pragma mark - Getter
- (NSString *)iconKey{
    return objc_getAssociatedObject(self, iconNameKey);
}

- (NSString *)placeHolderName{
    return objc_getAssociatedObject(self, placeHolderNameKey);
}

- (UIControlState)controlState{
    NSNumber *number = objc_getAssociatedObject(self, controlStateKey);
    return [number unsignedIntegerValue];
}

@end
